package com.permission.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.permission.dao.IUserDao;
import com.permission.entity.User;
import com.permission.service.IUserService;
import com.wang.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户服务的实现类
 *
 * @author my
 * @date 2020/8/4 17:14
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User, IUserDao> implements IUserService {

    @Autowired
    private IUserDao userDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());;
    @Override
    public User login(String username, String password) {
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            logger.warn("login()---> username or password is null");
            return null;
        }
        logger.info("login()---> username:"+username+",password:"+password);

        Map<String, Object> where=new HashMap<>();
        where.put("username",username);
        User user = userDao.query(where);
        if (user!=null){
            if (password.equals(user.getPassword())){
                return user;
            }
        }
        return null;
    }

    @Override
    public IUserDao getDaoImpl() {
        return userDao;
    }

    @Override
    public User query(Integer id) {
        if (id==null||id<0){
            return null;
        }
        Map<String, Object> where=new HashMap<>();
        where.put("id",id);
        return getDaoImpl().query(where);
    }
}
